package com.smart.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.global.GlobalWord;
import com.smart.pojo.LeaveBill;
import com.smart.pojo.ReimbursementBill;
import com.smart.mapper.ReimbursementBillMapper;
import com.smart.resp.LeaveBillResp;
import com.smart.resp.ReimbursementBillResp;
import com.smart.service.IReimbursementBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.utils.CopyUtil;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-05-11
 */
@Service
public class ReimbursementBillServiceImpl extends ServiceImpl<ReimbursementBillMapper, ReimbursementBill> implements IReimbursementBillService {
    @Autowired
    private TaskService taskService;

    @Override
    public ReimbursementBillResp queryByBillId(long parseLong) {
        return baseMapper.queryByBillId(parseLong);
    }

    @Override
    public void queryPageByUserId(Long userId, Page page) {
        this.lambdaQuery().eq(ReimbursementBill::getUserId, userId).page(page);
        List<ReimbursementBillResp> reimbursementBillRespList = CopyUtil.copyList(page.getRecords(), ReimbursementBillResp.class);
        for (ReimbursementBillResp reimbursementBillResp : reimbursementBillRespList) {
            String businessKey = GlobalWord.REIMBURSEMENT_KEY + "." + reimbursementBillResp.getId();
            Task task = taskService.createTaskQuery().processInstanceBusinessKey(businessKey).singleResult();

            reimbursementBillResp.setTaskName(task == null ? "已结束" : task.getName());

        }
        page.setRecords(reimbursementBillRespList);
    }
}
